<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: null,
      chartInstance: null,
      // 处理图表需要的数据
      colorArr: [
        ['#0BA82C', '#4FF778'],
        // ['#2E72BF', '#23E5E5'],
        ['#f1c40f', '#f39c12'],
        ['#5052EE', '#AB6EE5'],
      ],
      startValue: 0,
      endValue: 9,
      timer: null,
    }
  },
  mounted() {
    this.chartInit()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.timer)
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    chartInit() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      const initOption = {
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999',
            },
          },
        },
        label: {
          show: true,
          position: 'top',
        },
        grid: {
          top: '40%',
          left: '5%',
          bottom: '5%',
          right: '5%',
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        title: {
          text: '地区销售排行',
          left: 20,
          top: 20,
        },
        xAxis: {
          type: 'category',
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'bar',
            itemStyle: {
              barBorderRadius: [32, 32, 0, 0],
            },
          },
        ],
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timer)
      })
      this.chartInstance.on('mouseout', () => {
        this.startTimer()
      })
    },
    async getData() {
      const { data: res } = await this.$http.get('/rank')
      this.allData = res
      this.allData.sort((a, b) => b.value - a.value)
      this.updateChart()
      this.startTimer()
    },
    updateChart() {
      // 处理图表需要的数据
      const provinceArr = this.allData.map((item) => {
        return item.name
      })
      const valueArr = this.allData.map((item) => {
        return item.value
      })
      const dataOption = {
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        xAxis: {
          data: provinceArr,
        },
        yAxis: {
          max: valueArr[0] * 1.5,
        },
        series: [
          {
            data: valueArr,

            itemStyle: {
              opacity: 0.7,
              color: (arg) => {
                let targetColorArr = this.colorArr[0]
                if (arg.value >= 300) {
                  targetColorArr = this.colorArr[2]
                } else if (arg.value >= 200) {
                  targetColorArr = this.colorArr[1]
                } else {
                  targetColorArr = this.colorArr[0]
                }
                return new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ])
              },
            },
          },
          {
            type: 'line',
            data: valueArr.map((el) => el * 1.5),

            smooth: false,
          },
        ],
      }
      this.chartInstance.setOption(dataOption)
    },
    startTimer() {
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.updateChart()
      }, 3000)
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0.5 * titleFontSize, 0.5 * titleFontSize, 0, 0],
            },
          },
        ],
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
  },
}
</script>

<style></style>
